package br.com.sovi.gwt.client.controleunitario;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.sovi.gwt.client.controleunitario.service.ControleUnitarioService;
import br.com.sovi.gwt.client.controleunitario.service.ControleUnitarioServiceAsync;
import br.com.sovi.gwt.client.entity.ControleUnitario;
import br.com.sovi.gwt.client.entity.ControleUnitarioPoint;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Node;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;

public class ControleUnitarioTrafegoMenuPanel extends TreePanel {

	private SelectionListener selectionListener;

	private TreeNode rootNode;

	public ControleUnitarioTrafegoMenuPanel() {
		this.setTitle("Rastreamento no Tráfego");
		this.setHeight("100%");
		this.setWidth(200);
		this.setCollapsed(true);
		this.setAnimate(true);
		this.setEnableDD(true);
		this.setContainerScroll(true);
		this.setAutoScroll(true);
		this.setRootVisible(false);
		this.setFrame(true);

		this.expand();

		rootNode = new TreeNode();
		this.setRootNode(rootNode);

		updateControlesUnitarios();
	}

	public void createNewRootNode() {
		if (rootNode != null) {
			rootNode.destroy();
		}
		rootNode = new TreeNode();
		this.setRootNode(rootNode);
	}

	public void updateControlesUnitarios() {
		createNewRootNode();
		
		ControleUnitarioServiceAsync async = GWT.create(ControleUnitarioService.class);
		async.getAll(new AsyncCallback<List<ControleUnitario>>() {
			@Override
			public void onFailure(Throwable caught) {
				GWT.log(null, caught);
			}

			@Override
			public void onSuccess(List<ControleUnitario> points) {

				if (points == null) {
					GWT.log("No points found.", null);
					return;
				}

				Window.alert("Pontos: " + points.size());

				for (final ControleUnitario controleUnitario : points) {

					TreeNode treeNode = new TreeNode(controleUnitario.getDescricao());
					treeNode.addListener(new TreeNodeListenerAdapter() {
						@Override
						public void onClick(Node node, EventObject e) {
							if (getSelectionListener() != null) {
								getSelectionListener().onClick(controleUnitario);
							}
						}
					});

					rootNode.appendChild(treeNode);
				}

			}
		});
	}

	private List<ControleUnitario> getControleUnitarioList() {

		List<ControleUnitario> points = new ArrayList<ControleUnitario>();

		points.add(new ControleUnitario("", createDatePlusMinutes(1, 0), true, false, 11, new ControleUnitarioPoint[] {
				new ControleUnitarioPoint(createDatePlusMinutes(1, 0), -23.441356510980597, -51.93417549133301),
				new ControleUnitarioPoint(createDatePlusMinutes(1, 1), -23.436395364022726, -51.933488845825195),
				new ControleUnitarioPoint(createDatePlusMinutes(1, 2), -23.420801975641655, -51.9364085892334),
				new ControleUnitarioPoint(createDatePlusMinutes(1, 3), -23.41741528968557, -51.91872596740723) }));

		points.add(new ControleUnitario("", createDatePlusMinutes(1, 0), true, false, 11, new ControleUnitarioPoint[] {
				new ControleUnitarioPoint(createDatePlusMinutes(2, 0), -23.297328919616124, -51.20135307312012),
				new ControleUnitarioPoint(createDatePlusMinutes(2, 1), -23.30355653882127, -51.17182731628418),
				new ControleUnitarioPoint(createDatePlusMinutes(2, 2), -23.319951913844882, -51.1578369140625),
				new ControleUnitarioPoint(createDatePlusMinutes(2, 3), -23.339103564687345, -51.142473220825195) }));

		return points;
	}

	private Date createDatePlusMinutes(int day, int minutes) {
		return new Date(2009, 1, day, 10, minutes, 0);
	}

	public void setSelectionListener(SelectionListener selectionListener) {
		this.selectionListener = selectionListener;
	}

	public SelectionListener getSelectionListener() {
		return selectionListener;
	}

	public static interface SelectionListener {
		void onClick(ControleUnitario controleUnitario);
	}
}
